/**
 * Copyright (C) 2013 SHANGHAI VOLKSWAGEN, All rights reserved.
 * License version 1.0, a copy of which has been included with this.
 *
 * @File name：com.market.test.server.listener.SalariesListener
 * @Create on：2024/3/11
 * @Author：liuchang
 */
package com.market.test.server.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import com.alibaba.excel.read.listener.ReadListener;
import com.market.test.common.domain.ExcelData;
import com.market.test.server.mapper.ExcelMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Map;

/**
 * <p>ClassName：SalariesListener</p >
 * <p>Description：</p >
 * <p>Author：liuchang</p >
 * <p>Date：2024/3/11</p >
 */
@Component
@Log4j2
public class SalariesListener implements ReadListener<ExcelData>{
    //最大一次添加的数量
    private static final int BATCH_COUNT=1000;

    private ArrayList<ExcelData> list=new ArrayList<>();






    @Autowired
    private ExcelMapper excelMapper;

    /**
     * 这个每一条数据解析都会来调用
     * @param
     * @param
     */
    @Override
    public void invoke(ExcelData excelData, AnalysisContext analysisContext) {
        list.add(excelData);
        if (list.size()>=BATCH_COUNT){
           excelMapper.addBatch(list);
            list.clear();
        }
    }
    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
       log.info("开始插入...");
        excelMapper.addBatch(list);
    }
}
